// <copyright file="ColorPicker.cs" company="YUIAsp.Net">
// Copyright (c) 2009 All Right Reserved
// </copyright>
// <author>raul popescu</author>
// <email>dev@yuiasp.net</email>
// <date>2009-01-30</date>
// <summary>YUIAsp.Net File</summary>

using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using YUIAspNet.Common;

#region Embedded resources definitions
[assembly: WebResource("YUIAspNet.ColorPicker.ColorPicker.js", "text/javascript")]

[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.ColorPicker.colorpicker.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.ColorPicker.picker_thumb.png", "image/png")]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.ColorPicker.hue_thumb.png", "image/png")]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.ColorPicker.hue_bg.png", "image/png")]
[assembly: WebResource("YUIAspNet.EmbeddedSkins.DefaultSkin.ColorPicker.picker_mask.png", "image/png")] 
#endregion

namespace YUIAspNet
{
    /// <summary>
    /// Color picker control.
    /// </summary>
    [ToolboxItem(true)]
    [ParseChildren(true)]
    public class ColorPicker : YUIScriptBaseControl, INamingContainer
    {
        HiddenField _colorPersister = new HiddenField();
        Color _color = Color.White;

        /// <summary>
        /// Initializes a new instance of the <see cref="ColorPicker"/> class.
        /// </summary>
        public ColorPicker() : base("div")
        {
            //TagName = "div";
            this.Init += new EventHandler(ColorPicker_Init);
            this.PreRender += new EventHandler(ColorPicker_PreRender);
        }

        void ColorPicker_Init(object sender, EventArgs e)
        {
            this._colorPersister.ID = "colorPersister";
            this.Controls.Add(_colorPersister);
        }

        void ColorPicker_PreRender(object sender, EventArgs e)
        {
            //sync the color container.
            this._colorPersister.Value = string.Format(CultureInfo.InvariantCulture, "{0}|{1}|{2}", this.Color.R, this.Color.G, this.Color.B);

            this.Attributes.Add("style", "position:relative;");
        }

        /// <summary>
        /// Registers the style sheets for the current control.
        /// Must be overriden in the parent control to add any css file reference.
        /// </summary>
        protected override void RegisterStyleSheets()
        {
            this.RegisterEmbeddedStylesheet("ColorPicker", "colorpicker.css");
        }

        /// <summary>
        /// Register the values to send to the client control for initialization.
        /// </summary>
        protected override void RegisterClientInitialization() 
        {
            AddProperty("ColorPersister", this._colorPersister.ClientID);

            AddProperty("ShowControls", this.ShowControls);
            AddProperty("ShowHexControls", this.ShowHexControls);
            AddProperty("ShowHexSummary", this.ShowHexSummary);
            AddProperty("ShowHsvControls", this.ShowHsvControls);
            AddProperty("ShowWebsafe", this.ShowWebsafe);
            AddProperty("ShowRgbControls", this.ShowRgbControls);

            if (PickerThumbImageUrl == null)
                AddProperty("PickerThumbImageUrl", this.FindSkinImage("ColorPicker", "picker_thumb.png"));
            else
                AddProperty("PickerThumbImageUrl", this.PickerThumbImageUrl);

            if (HueThumbImageUrl == null)
                AddProperty("HueThumbImageUrl", this.FindSkinImage("ColorPicker", "hue_thumb.png"));
            else
                AddProperty("HueThumbImageUrl", this.HueThumbImageUrl);

            AddProperty("Red", this.Red);
            AddProperty("Green", this.Green);
            AddProperty("Blue", this.Blue);

            if (!string.IsNullOrEmpty(OnClientChange))
            {
                AddEvent("OnChange", OnClientChange);
            }
        }

        /// <summary>
        /// Registers any additional scripts to include for this control.
        /// </summary>
        protected override void RegisterScriptReferences()
        { 
            AddScriptReference(new ScriptReference("YUIAspNet.ColorPicker.ColorPicker.js", this.GetType().Assembly.FullName));
        }

        #region Public properties
        /// <summary>
        /// Show / hide the controls.
        /// </summary>
        public bool ShowControls { get; set; }

        /// <summary>
        /// Show / hide the hex controls
        /// </summary>
        public bool ShowHexControls { get; set; }

        /// <summary>
        /// Show / hide the hex summary container
        /// </summary>
        public bool ShowHexSummary { get; set; }

        /// <summary>
        /// Show / hide the hsv controls
        /// </summary>
        public bool ShowHsvControls { get; set; }

        /// <summary>
        /// Show / hide the container where closest websafe color is displayed
        /// </summary>
        public bool ShowWebsafe { get; set; }

        /// <summary>
        /// Show / hide the containers where the RGB color is displayed
        /// </summary>
        public bool ShowRgbControls { get; set; }

        /// <summary>
        /// Get / set the selected color
        /// </summary>
        public Color Color
        {
            get
            {
                
                if (this._colorPersister.Value.Length > 3)
                {
                    string[] colComponent = this._colorPersister.Value.Split('|');
                    this._color = Color.FromArgb(Convert.ToInt32(colComponent[0], CultureInfo.InvariantCulture), Convert.ToInt32(colComponent[1], CultureInfo.InvariantCulture), Convert.ToInt32(colComponent[2], CultureInfo.InvariantCulture));
                }

                return _color;
            }
            set
            {
                this._color = value;
                this._colorPersister.Value = string.Format(CultureInfo.InvariantCulture, "{0}|{1}|{2}", value.R, value.G, value.B);
            }
        }

        /// <summary>
        /// Gets the Red component of the current color
        /// </summary>
        protected byte Red
        {
            get { return this.Color.R; }
        }

        /// <summary>
        /// Gets the Green component of the current color
        /// </summary>
        protected byte Green
        {
            get { return this.Color.G; }
        }

        /// <summary>
        /// Gets the Blue component of the current color
        /// </summary>
        protected byte Blue
        {
            get { return this.Color.B; }
        }

        /// <summary>
        /// Gets or sets the picker thumb image URL.
        /// </summary>
        /// <value>The picker thumb image URL.</value>
        public Uri PickerThumbImageUrl { get; set; }

        /// <summary>
        /// Gets or sets the hue thumb image URL.
        /// </summary>
        /// <value>The hue thumb image URL.</value>
        public Uri HueThumbImageUrl { get; set; }

        /// <summary>
        /// Gets or sets the on client change.
        /// </summary>
        /// <value>The on client change.</value>
        public string OnClientChange { get; set; } 
        #endregion

     }
}
